package com.dq.context.listener;

import com.dq.cache.J2CacheUtils;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import lombok.Data;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/12/2 0002 11:42:19
 */
@Component
public class ApiInitDataListener extends AllService implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        customerAnalysisTime();
    }

    /**
     * 初始化用户分析查询条件
     */
    public void customerAnalysisTime(){
        if(!J2CacheUtils.exists("CustomerType_Active")){
            List<CustomerTimeQuery> customerTimeQueryList =new ArrayList<>();
            CustomerTimeQuery customerTimeQuery1 = new CustomerTimeQuery();
            customerTimeQuery1.setName("一月内");
            customerTimeQuery1.setQueryTimestamp(TimeUtil.getLastMonthNowTimestamp(-1));
            CustomerTimeQuery customerTimeQuery2 = new CustomerTimeQuery();
            customerTimeQuery2.setName("二月内");
            customerTimeQuery2.setQueryTimestamp(TimeUtil.getLastMonthNowTimestamp(-2));
            CustomerTimeQuery customerTimeQuery3 = new CustomerTimeQuery();
            customerTimeQuery3.setName("三月内");
            customerTimeQuery3.setQueryTimestamp(TimeUtil.getLastMonthNowTimestamp(-3));
            J2CacheUtils.put("CustomerType_Active",customerTimeQueryList);
        }
        if(!J2CacheUtils.exists("CustomerType_Silent")){
            List<CustomerTimeQuery> customerTimeQueryList =new ArrayList<>();
            CustomerTimeQuery customerTimeQuery1 = new CustomerTimeQuery();
            customerTimeQuery1.setName("三月前");
            customerTimeQuery1.setQueryTimestamp(TimeUtil.getLastMonthNowTimestamp(-3));
            CustomerTimeQuery customerTimeQuery2 = new CustomerTimeQuery();
            customerTimeQuery2.setName("六月前");
            customerTimeQuery2.setQueryTimestamp(TimeUtil.getLastMonthNowTimestamp(-6));
            CustomerTimeQuery customerTimeQuery3 = new CustomerTimeQuery();
            customerTimeQuery3.setName("一年前");
            customerTimeQuery3.setQueryTimestamp(TimeUtil.getLastYearNowTimestamp(-1));
            J2CacheUtils.put("CustomerType_Silent",customerTimeQueryList);
        }
    }

    @Data
    public class CustomerTimeQuery{
        public Long queryTimestamp;
        public String name;
    }

}
